﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class GiaBanDinhMuc : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmGiaBanDinhMuc";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static int PageNumber = 10;
    static int CurrentPage = 1;
    static DataTable dt;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 1; i <= 12; i++)
            {
                dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                dlThangLoc.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
            }
            dlThang.Items.Insert(0, new ListItem("Chọn tháng", ""));
            dlThang.SelectedIndex = 0;

            dlThangLoc.Items.Insert(0, new ListItem("Chọn tháng", "0"));
            dlThangLoc.SelectedIndex = 0;

            int year1 = DateTime.Now.Year - 3;
            int year2 = DateTime.Now.Year;
            for (int i = year2; i >= year1; i--)
            {
                dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                dlNamLoc.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
            }
            dlNam.SelectedValue = year2.ToString();
            dlNamLoc.SelectedValue = year2.ToString();
            Title = "Giá bán định mức | " + Header;
            btnCancel_Click(sender, e);
        }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        btnNew.Enabled = true;

        btnNext.Visible = true;
        btnFirst.Visible = true;
        btnPre.Visible = true;


        dlThang.SelectedIndex = 0;
        dlNam.SelectedIndex = 0;

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiabandinhmuc_List",p);
        GV.DataBind();
        FirstGridViewRow();
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdagiabandinhmuc_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiabandinhmuc_List", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiabandinhmuc_List", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", ex.GetGuid(ID));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            dt = ex.GetData("sp_xdagiabandinhmuc_ListByID", p);
            if (dt.Rows.Count > 0)
            {

                DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[0].Cells[1].FindControl("dlLoaiVL");
                TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[0].Cells[2].FindControl("txtGiaCoVAT");
                TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[0].Cells[3].FindControl("txtGiaKhongVAT");


                GridView1.Rows[0].Cells[0].Text = "1";

                dlLoaiVL.SelectedValue = dt.Rows[0]["LoaiVL"].ToString().ToLower();
                txtGiaCoVAT.Text = dt.Rows[0]["GiaCoVAT"].ToString().ToLower();
                txtGiaKhongVAT.Text = dt.Rows[0]["GiaKhongVAT"].ToString().ToLower();
                dlThang.SelectedValue = dt.Rows[0]["Thang"].ToString().ToLower();
                dlNam.SelectedValue = dt.Rows[0]["Nam"].ToString().ToLower();
            }
            btnSave.Visible=false;
            btncapnhat.Visible=true;
            btnNew.Enabled=false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                ex.ExecuteNonQuerry("sp_xdagiabandinhmuc_Del", p);
                btnCancel_Click(sender,e);
                gstGetMess("Xóa thành công","");
            }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();

                for (int i = 0; i < GridView1.Rows.Count - 1; i++)
                {
                    GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("dlLoaiVL");
                    TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtGiaCoVAT");
                    TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtGiaKhongVAT");
                    
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["LoaiVL"] = dlLoaiVL.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["GiaCoVAT"] = txtGiaCoVAT.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["GiaKhongVAT"] = txtGiaKhongVAT.Text.Trim();
                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
            }
        }
        
    }
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("LoaiVL", typeof(string)));
        dt.Columns.Add(new DataColumn("GiaCoVAT", typeof(string)));
        dt.Columns.Add(new DataColumn("GiaKhongVAT", typeof(string)));
        

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["LoaiVL"] = string.Empty;
        dr["GiaCoVAT"] = string.Empty;
        dr["GiaKhongVAT"] = string.Empty;
        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;


        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("dlLoaiVL");
                    TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtGiaCoVAT");
                    TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtGiaKhongVAT");
                    
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["LoaiVL"] = dlLoaiVL.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["GiaCoVAT"] = txtGiaCoVAT.Text.Trim().Equals("") ? "0" : txtGiaCoVAT.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["GiaKhongVAT"] = txtGiaKhongVAT.Text.Trim().Equals("") ? "0" : txtGiaKhongVAT.Text.Trim();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GridView1.DataSource = dtCurrentTable;
                GridView1.DataBind();
            }
        }
        SetPreviousData();
    }

    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("dlLoaiVL");
                    TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txtGiaCoVAT");
                    TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txtGiaKhongVAT");


                    GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    
                    dlLoaiVL.SelectedValue = dt.Rows[i]["LoaiVL"].ToString();
                    txtGiaCoVAT.Text = dt.Rows[i]["GiaCoVAT"].ToString();
                    txtGiaKhongVAT.Text = dt.Rows[i]["GiaKhongVAT"].ToString();
                    
                    rowIndex++;
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
            gstGetMess("Chọn tháng", "");
        else
        {
            object ID;
            string giacovat = "", giakhongvat = "";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("dlLoaiVL");
                TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[i].Cells[2].FindControl("txtGiaCoVAT");
                TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txtGiaKhongVAT");

                ID = ex.GetGuid(dlLoaiVL.SelectedValue);
                giacovat = txtGiaCoVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtGiaCoVAT.Text.Trim().Replace(",", "");
                giakhongvat = txtGiaKhongVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtGiaKhongVAT.Text.Trim().Replace(",", "");

                p = new SqlParameter[6];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@LoaiVL", ID);
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;

                p[2] = new SqlParameter("@GiaCoVAT", double.Parse(giacovat));
                p[2].SqlDbType = SqlDbType.Float;
                p[3] = new SqlParameter("@GiaKhongVAT", double.Parse(giakhongvat));
                p[3].SqlDbType = SqlDbType.Float;

                p[4] = new SqlParameter("@Thang", int.Parse(dlThang.SelectedValue));
                p[4].SqlDbType = SqlDbType.Int;
                p[5] = new SqlParameter("@Nam", int.Parse(dlNam.SelectedValue));
                p[5].SqlDbType = SqlDbType.Int;

                ex.ExecuteNonQuerry("sp_xdagiabandinhmuc_Add", p);
                
            }
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
            gstGetMess("Chọn tháng", "");
        else
        {
            int i = 0;
            DropDownList dlLoaiVL = (DropDownList)GridView1.Rows[i].Cells[1].FindControl("dlLoaiVL");
            TextBox txtGiaCoVAT = (TextBox)GridView1.Rows[i].Cells[2].FindControl("txtGiaCoVAT");
            TextBox txtGiaKhongVAT = (TextBox)GridView1.Rows[i].Cells[3].FindControl("txtGiaKhongVAT");

            object ID = ex.GetGuid(dlLoaiVL.SelectedValue);
            string giacovat = txtGiaCoVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtGiaCoVAT.Text.Trim().Replace(",", "");
            string giakhongvat = txtGiaKhongVAT.Text.Trim().Replace(",", "").Equals("") ? "0" : txtGiaKhongVAT.Text.Trim().Replace(",", "");


            Guid gID = new Guid(hdID.Value);
            p = new SqlParameter[6];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@LoaiVL", ID);
            p[1].SqlDbType = SqlDbType.UniqueIdentifier;

            p[2] = new SqlParameter("@GiaCoVAT", double.Parse(giacovat));
            p[2].SqlDbType = SqlDbType.Float;
            p[3] = new SqlParameter("@GiaKhongVAT", double.Parse(giakhongvat));
            p[3].SqlDbType = SqlDbType.Float;

            p[4] = new SqlParameter("@Thang", int.Parse(dlThang.SelectedValue));
            p[4].SqlDbType = SqlDbType.Int;
            p[5] = new SqlParameter("@Nam", int.Parse(dlNam.SelectedValue));
            p[5].SqlDbType = SqlDbType.Int;

            ex.ExecuteNonQuerry("sp_xdagiabandinhmuc_Update", p);
            btnCancel_Click(sender, e);
            gstGetMess("Lưu thành công", "");
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@Thang", int.Parse(dlThangLoc.SelectedValue));
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@Nam", int.Parse(dlNamLoc.SelectedValue));
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdagiabandinhmuc_Search", p);
        GV.DataBind();

        btnNext.Visible = false;
        btnFirst.Visible = false;
        btnPre.Visible = false;
    }
}